<?php
// 应用公共文件

/**
 * [list_local_app 获取所有的插件列表]
 * @return [type] [description]
 */
function list_local_app()
{
    $app_root   = app()->appin_path;
    $data       = scandir($app_root);
    $countData  = count($data);
    $appins = [];
    for( $i=0; $i < $countData; $i++ ) {
        if ($data[$i] != '.' && $data[$i] != '..' && stripos($data[$i], '.php') === false) {
            $appins[] = include_once( $app_root . '/' . $data[$i] . '/app.php');
        }
    }
    return $appins;
}

/**
 * [install_app 安装应用]
 * @param  [type] $tag_name [description]
 * @param  [type] $config   [description]
 * @return [type]           [description]
 */
function install_app($tag_name)
{
    return \app\appin\service\Package::install($tag_name);
}

/**
 * 返回上传文件的URL
 * @param {Object} $path
 */
function upload_url( $path )
{
	if ( 
		stripos($path, 'http://') !== false 
		|| stripos($path, 'https://') !== false
	 ) {
		 return $path;
	 } else {
		 return \app\common\model\System::getItemByType(2)['image_url'] . '/' . $path;
	 }
}

/**
 * 将所有需要缓存的数据放在回调内
 */
function cache_dm($cache_key, $cache_tag = '', callable $callback = null, $cache = 1)
{
	$systemConf = \app\common\model\System::getItemByType(1);
	$cache_time = false;
	if ( isset($systemConf['cache']) && (int)$systemConf['cache'] === 1) {
		$cache_time = (int) $systemConf['cache_time'];
	}
	
	//系统开启了缓存并且设置了缓存时间
	if ($cache_time > 0 && $cache === 1) {
		$cacheData = \think\facade\Cache::get($cache_key);
		if (!$cacheData) {
			$data = call_user_func($callback);
			$cacheData = serialize($data);
			 \think\facade\Cache::tag($cache_tag)->set($cache_key, $cacheData, $cache_time);
		}
		return unserialize($cacheData);
	} else {
		return call_user_func($callback);
	}
}

/**
 * 删除指定目录下的目录与文件
 * @param $path
 */
function deldir($path){
    if(is_dir($path)){
        $p = scandir($path);
        foreach($p as $val){
            if($val !="." && $val !=".."){
                if(is_dir($path.$val)){
                    deldir($path.$val.'/');
                    @rmdir($path.$val.'/');
                }else{
                    unlink($path.$val);
                }
            }
        }
    }
}

/**
 * 获取当前访问的URL
 * @return string
 */
function cur_uri()
{
    $url = 'http://';
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        $url = 'https://';
    }
    // 判断端口
    if($_SERVER['SERVER_PORT'] != '80') {
        $url .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . ':' . $_SERVER['REQUEST_URI'];
    } else {
        $url .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    }
    return $url;
}

/**
 * 判断是否是微信浏览器浏览
 * @return bool
 */
function is_weixin()
{
    $ua = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($ua, 'MicroMessenger') == false && strpos($ua, 'Windows Phone') == false) {
        return false;
    }
    return true;
}

/**
 * 微信登陆
 * @param $redirect
 */
function weixin_web_login($redirect)
{
    $weixinAppid = \app\admin\model\System::getItem('weixin_appid');
    $weixinJumpUri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
    $jumpUri = sprintf($weixinJumpUri, $weixinAppid, urlencode($redirect) );
    echo "<script>";
    echo "window.location.href = ".$jumpUri;
    echo "</script>";
    die('正在跳转微信登陆中......');
}

/**
 * 网站图片处理
 * @param $src
 * @return string
 */
function image($src)
{
    if (stripos($src, 'http://') === false && stripos($src, 'https://') === false) {
        $imageUrl = \app\admin\model\System::getItem('image_url');
        return rtrim($imageUrl, '/') . '/' . $src;
    }
    return $src;
}
